package com.zy.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zy.business.domain.TbDocument;
import com.zy.business.domain.TbDocumentFolder;
import com.zy.business.mapper.DocumentFolderMapper;
import com.zy.business.mapper.DocumentMapper;
import com.zy.business.service.DocumentFolderService;
import com.zy.business.service.DocumentService;
import com.zy.common.exception.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @description: 文档库实现类
 * @author: whf
 * @create: 2023-02-24 15:01
 **/
@Service
public class DocumentFolderServiceImpl extends ServiceImpl<DocumentFolderMapper, TbDocumentFolder> implements DocumentFolderService {

    @Autowired
    private DocumentMapper documentMapper;

    @Override
    public int addDocumentFolder(TbDocumentFolder documentFolder) {
        documentFolder.setCreateTime(new Date());
        int row = baseMapper.insert(documentFolder);
        return row;
    }

    @Override
    public int editDocumentFolder(TbDocumentFolder documentFolder) {
        documentFolder.setUpdateTime(new Date());
        int row = baseMapper.updateById(documentFolder);
        return row;
    }

    @Override
    public int deleteDocumentFolder(Long id) {
        Long num = documentMapper.selectCount(new QueryWrapper<TbDocument>().eq("document_folder_id", id));
        if (num!=0){
            throw new ServiceException("该文件夹下存在文件，无法删除");
        }
        int row = baseMapper.deleteById(id);
        return row;
    }

    @Override
    public List<TbDocumentFolder> list(Long projectId) {
        return baseMapper.list(projectId);
    }
}
